package com.lzy.crm.wechat.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.protobuf.GeneratedMessageV3;
import com.googlecode.protobuf.format.JsonFormat;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.CreateRoomInfo;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdApply;
import com.lzy.crm.domain.CrowdRule;
import com.lzy.crm.domain.CrowdUser;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.proto.Group.CommMsg;
import com.lzy.crm.proto.Group.PaiQuanList_Info_Record;
import com.lzy.crm.proto.Group.PaiQuanRoomList_Info_Record;
import com.lzy.crm.proto.Group.PaiQuanRoom_Info;
import com.lzy.crm.proto.Group.PaiQuan_Info;
import com.lzy.crm.proto.Group.QuanGameRuleMsg;
import com.lzy.crm.proto.Group.QuanGameRuleMsgList;
import com.lzy.crm.proto.Group.QuanMember_Message;
import com.lzy.crm.proto.Group.QuanMember_Message_Record;
import com.lzy.crm.proto.Group.paiquan_player_info;
import com.lzy.crm.proto.Group.quer_paiquan_player_info_record;
import com.lzy.crm.service.CreateRoomInfoService;
import com.lzy.crm.service.CrowdApplyService;
import com.lzy.crm.service.CrowdFlowService;
import com.lzy.crm.service.CrowdRuleService;
import com.lzy.crm.service.CrowdService;
import com.lzy.crm.service.CrowdUserService;
import com.lzy.crm.service.PpUserService;
import com.lzy.crm.wechat.consts.CrowdConstants;
import com.lzy.crm.wechat.consts.GmConstants;

/**
 * 牌友圈接口 功能
 * 
 * @author csp
 *
 */
@Controller
@RequestMapping("/gm")
public class GmCrowdController {

	Logger logger = Logger.getLogger(GmCrowdController.class);

	@Resource
	private CrowdService crowdService;

	@Resource
	private CrowdUserService crowdUserService;

	@Resource
	private CrowdApplyService crowdApplyService;
	@Resource
	private CrowdRuleService crowdRuleService;

	@Resource
	private CrowdFlowService crowdFlowService;

	@Resource
	private PpUserService pUserService;

	@Resource
	private CreateRoomInfoService createRoomInfoService;
	
	//
	public static String resp(GeneratedMessageV3 build){
		return JsonFormat.printToString(build);
	}
	/**
	 * 群消息
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/message", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String message(HttpServletRequest request, ModelMap model) {

		com.lzy.crm.proto.Group.QuanMember_Message_Record.Builder quanMember_Message_Record = QuanMember_Message_Record
				.newBuilder();
		String gameId = request.getParameter("uid");

		// 玩家申请加入
		List<CrowdApply> crowdApplyList = crowdApplyService
				.getCrowdApplyListByGameId(gameId);
		for (CrowdApply crowdApply : crowdApplyList) {
			com.lzy.crm.proto.Group.QuanMember_Message.Builder quanMember_Message = QuanMember_Message
					.newBuilder();
			quanMember_Message.setMessageID(crowdApply.getId());
			quanMember_Message.setJoinUserID(crowdApply.getGameId());
			quanMember_Message.setQuanName(crowdApply.getCrowdName());
			quanMember_Message.setQuanID(crowdApply.getCrowdId());
			quanMember_Message.setJoinUserName(crowdApply.getGameNickName());
			quanMember_Message.setIsAgree(Integer.parseInt(crowdApply
					.getStatus()));
			quanMember_Message.setCrowdOwner(1);

			quanMember_Message_Record.addMessages(quanMember_Message);
		}

		// 我申请的加入
		List<CrowdApply> myCrowdApplyList = crowdApplyService
				.getMyCrowdApplyListByGameId(gameId);

		for (CrowdApply crowdApply : myCrowdApplyList) {
			com.lzy.crm.proto.Group.QuanMember_Message.Builder quanMember_Message = QuanMember_Message
					.newBuilder();
			quanMember_Message.setMessageID(crowdApply.getId());
			quanMember_Message.setJoinUserID(crowdApply.getGameId());
			quanMember_Message.setQuanName(crowdApply.getCrowdName());
			quanMember_Message.setQuanID(crowdApply.getCrowdId());
			quanMember_Message.setJoinUserName(crowdApply.getGameNickName());
			quanMember_Message.setIsAgree(Integer.parseInt(crowdApply
					.getCrowdStatus()));
			quanMember_Message.setCrowdOwner(0);
			quanMember_Message_Record.addMessages(quanMember_Message);
		}


		return resp(quanMember_Message_Record.build());
	}

	/**
	 * 群消息处理
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/message_op", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String message_op(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		String uid = request.getParameter("uid");
		String crowdId = request.getParameter("groupid");
		String crowdApplyId = request.getParameter("msgid");
		String type = request.getParameter("type");
		PpUser puser = pUserService.getPpUser(uid);
		try {
			// 修改申请信息
			crowdApplyService.updateCrowdApply(crowdApplyId, type, "app端操作",
					puser);
			commMsg.setCode(1);
			commMsg.setMsg("操作成功~");

		} catch (Exception e) {
			commMsg.setCode(2);
			commMsg.setMsg("操作失败~");
			logger.error("群消息处理失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 创建牌圈
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_create", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String doAddUser(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();

		try {

			String uid = request.getParameter("uid");

			PpUser user = pUserService.getPpUser(uid);

			String crowdName = request.getParameter("name");
			String remark = request.getParameter("desc");
			
			if(user==null){
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				commMsg.setMsg("数据有误");
				return resp(commMsg.build());
			}
			// 创建牌友圈
			int result = crowdService.addCrowd(crowdName, remark, user);
			commMsg.setCode(result);
			if (result == 1) {
				commMsg.setMsg("操作成功");
			} else if (result == 2) {
				commMsg.setMsg("该功能只对代理开放~");

			} else {
				commMsg.setMsg("您创建的牌圈数量已达最大数量，请先删除没用的牌圈后再试~");
			}

		} catch (Exception e) {
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			commMsg.setMsg("创建牌圈失败");
			logger.error("创建牌圈失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 删除牌友群
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_destroy", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_del(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {

			String uid = request.getParameter("uid");
			String crowdId = request.getParameter("groupid");
			// 删除牌友群关系
			// crowdUserService.delCrowdUserByCrowdId(crowdId);
			// 删除牌友群
			Crowd crowd = crowdService.getById(crowdId);
			if (Integer.parseInt(uid) == crowd.getGameId()) {
				// 设置删除状态
				crowd.setStatus(CrowdConstants.crowd_status_del);
				crowdService.updateByPrimaryKeySelective(crowd);
				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
			} else {
				commMsg.setMsg(" 权限不够~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			}
		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改个人信息失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 牌圈增加用户
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_jion", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String crowdUser_add_save(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {
			String groupId = request.getParameter("groupid");
			String uid = request.getParameter("uid");
			
			if (crowdUserService.getCrowdUser(groupId, uid)) {
				commMsg.setMsg(" 该玩家已是俱乐部成员~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				return resp(commMsg.build());
			}
			Crowd crowd = crowdService.getById(groupId);
			if (crowd == null) {
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				commMsg.setMsg("该亲友圈不存在~ ");
				return resp(commMsg.build());
			}

			PpUser user = pUserService.getPpUser(uid);
			if (StringUtil.equals(crowd.getIsCard(), "0")) {

				Integer result = crowdApplyService.applyCrowd(Integer.valueOf(groupId), "", user);
				if (result == 0) {
					commMsg.setMsg("数据错误~ ");
					commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				} else if (result == 1) {
					commMsg.setMsg("申请已提交，请通知群主在俱乐部管理里点同意即可~  ");
					commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				}else{
					commMsg.setMsg("申请已提交，请通知群主在俱乐部管理里点同意即可~  ");
					commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				}
				return resp(commMsg.build());
			} else {
				// 直接加群
				// 创建牌友圈
				crowdUserService
						.addCrowdUserByPlayerInfo(groupId, "", user);
			}
			commMsg.setMsg(" 该玩家已是俱乐部成员~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			commMsg.setMsg(" 数据错误，请稍候再试~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("牌圈增加用户", e);
		}

		return resp(commMsg.build());
	}

	/**
	 * 设置牌圈是否可直接加入
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_enter_set", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String group_enter_set(HttpServletRequest request, ModelMap model) {

		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {

			String uid = request.getParameter("uid");
			String crowdId = request.getParameter("groupid");
			String pwd = request.getParameter("pwd");
			String type = request.getParameter("type");

			//
			Crowd crowd = crowdService.getById(crowdId);
			if (Integer.parseInt(uid) == crowd.getGameId()) {
				// 修改群状态
				crowd.setIsCard(type);
				crowdService.updateByPrimaryKeySelective(crowd);
				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
			} else {
				commMsg.setMsg(" 权限不够~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			}
		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改群信息失败", e);
		}
		return resp(commMsg.build());

	}

	/**
	 * 牌圈移除群玩家
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "/crowdUser_del", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String crowdUser_del(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {
			String crowdUserId = request.getParameter("crowdUserId");
			CrowdUser crowdUser = crowdUserService.getById(crowdUserId);
			// 删除牌友群 玩家
			crowdUserService.deleteByPrimaryKey(crowdUserId);

			crowdUserService.updateCrowdUserCount(crowdUser.getCrowdId()
					.toString());

			commMsg.setMsg(" 操作成功~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);

		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("移除玩家失败", e);
		}
		return resp(commMsg.build());
	}
	
	/**
	 * 牌圈移除群玩家
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "/exit_crowd", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String exit_crowd(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {
			String uid = request.getParameter("uid");
			String crowdid = request.getParameter("crowdid");
			
			CrowdUser crowdUser = crowdUserService.getCrowdUserByCrowdIdAndGameId(crowdid, uid);
			// 删除牌友群 玩家
			crowdUserService.deleteByPrimaryKey(crowdUser.getId());

			crowdUserService.updateCrowdUserCount(crowdUser.getCrowdId()
					.toString());

			commMsg.setMsg(" 操作成功~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);

		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("移除玩家失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 修改群名称
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_name_set", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String group_name_set(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {

			String uid = request.getParameter("uid");
			String crowdId = request.getParameter("groupid");
			String pwd = request.getParameter("pwd");
			String name = request.getParameter("name");

			// 删除牌友群
			Crowd crowd = crowdService.getById(crowdId);
			if (crowd == null) {
				commMsg.setMsg(" 群已不存在~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				return resp(commMsg.build());
			}

			if (Integer.parseInt(uid) == crowd.getGameId()) {
				// 修改群状态
				crowd.setCrowdName(name);
				crowdService.updateByPrimaryKeySelective(crowd);
				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
			} else {
				commMsg.setMsg(" 权限不够~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			}
		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改群信息失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 设置群规则
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	// http://192.168.3.174:8080/crm/gm/group/add_group_setting.html?groupId=102&rule=4344465
	@RequestMapping(path = "group/group_setting", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String update_group_setting(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {
			String uid = request.getParameter("uid");
			String crowdId = request.getParameter("groupid");
			String ruleid = request.getParameter("ruleid");
			String desc = request.getParameter("desc");
			String rule = request.getParameter("createdata");
			if(ruleid == null){
				int rs = crowdRuleService.addRuleCrowd(Integer.parseInt(crowdId), desc, rule);
				if(rs == 1){
					commMsg.setMsg(" 操作成功~ ");
					commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				}else{
					commMsg.setMsg(" 亲友圈不存在~ ");
					commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				}
				
				return resp(commMsg.build());
			}
			CrowdRule crowdRule = crowdRuleService.getById(ruleid);
			crowdRule.setRemark(desc);
			crowdRule.setRule(rule);
			crowdRuleService.updateByPrimaryKeySelective(crowdRule);
			commMsg.setMsg(" 操作成功~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);

		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改群信息失败", e);
		}
		return resp(commMsg.build());
	}
	
	/**
	 * 设置群规则
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	// http://192.168.3.174:8080/crm/gm/group/group_enable.html?groupId=102&group_enable=4344465
	@RequestMapping(path = "group/group_enable", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String group_enable(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {
			String uid = request.getParameter("uid");
			String crowdId = request.getParameter("groupid");
			String enable = request.getParameter("enable");
			
			Crowd crowd = crowdService.getById(crowdId);
			// 修改群状态
			crowd.setGameRule(enable.equals("true")?1:0);
			crowdService.updateByPrimaryKeySelective(crowd);
			commMsg.setMsg(" 操作成功~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);

		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改群信息失败", e);
		}
		return resp(commMsg.build());
	}
	/**
	 * 群成员操作
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/member_op", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String member_op(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.CommMsg.Builder commMsg = CommMsg.newBuilder();
		try {

			String crowdId = request.getParameter("groupid");
			String uid = request.getParameter("uid"); // 发起的玩家ID
			//String pwd = request.getParameter("pwd");
			//String name = request.getParameter("name");
			String op_uid = request.getParameter("op_uid"); // 要操作的玩家ID
			String op = request.getParameter("op"); // 要操作的状态
			if (crowdId == null) {
				commMsg.setMsg(" 数据错误~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				return resp(commMsg.build());
			}
			// 删除牌友群
			Crowd crowd = crowdService.getById(crowdId);

			CrowdUser crowdUserSp = crowdUserService
					.getCrowdUserByCrowdIdAndGameId(crowdId, uid);

			CrowdUser crowdUser = crowdUserService
					.getCrowdUserByCrowdIdAndGameId(crowdId, op_uid);
			if (crowd == null || crowdUserSp == null || crowdUser == null) {
				commMsg.setMsg(" 数据错误~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
				return resp(commMsg.build());
			}
			// 1删除，2取消管理，3设为管理，4设为群主
			if (StringUtil.equals("1", op)) {
				// 操作人 是群主 或者被操作对象是普通用户
				if (StringUtil.equals(crowdUserSp.getIsManager(),
						CrowdConstants.crowd_is_manage_create)
						|| StringUtil.equals(crowdUser.getIsManager(),
								CrowdConstants.crowd_is_manage_user)) {
					crowdUserService.deleteByPrimaryKey(crowdUser.getId());
					crowdUserService.updateCrowdUserCount(crowdId);
					commMsg.setMsg(" 操作成功~ ");
					commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
					return resp(commMsg.build());
				} else {
					commMsg.setMsg(" 操作成功~ ");
					commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
					return resp(commMsg.build());
				}

			}

			if (StringUtil.equals("2", op)
					&& !StringUtil.equals(crowdUserSp.getIsManager(),
							CrowdConstants.crowd_is_manage_user)) {
				crowdUser.setIsManager(CrowdConstants.crowd_is_manage_user);
				crowdUserService.updateByPrimaryKeySelective(crowdUser);
				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				return resp(commMsg.build());
			} else if (StringUtil.equals("3", op)
					&& StringUtil.equals(crowdUserSp.getIsManager(),
							CrowdConstants.crowd_is_manage_create)) {
				crowdUser.setIsManager(CrowdConstants.crowd_is_manage);
				crowdUserService.updateByPrimaryKeySelective(crowdUser);
				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				return resp(commMsg.build());
			}

			if (StringUtil.equals("4", op)
					&& StringUtil.equals(crowdUserSp.getIsManager(),
							CrowdConstants.crowd_is_manage_create)) {
				crowdUser.setIsManager(CrowdConstants.crowd_is_manage_create);
				crowdUser.setRemark(crowdUserSp.getGameId() + " 转让");
				crowdUserService.updateByPrimaryKeySelective(crowdUser);

				crowdUserSp.setIsManager(CrowdConstants.crowd_is_manage_user);
				crowdUserSp.setRemark("群主已转让给" + crowdUser.getGameId());
				crowdUserService.updateByPrimaryKeySelective(crowdUserSp);

				crowd.setOutUserName(crowdUser.getOutUserName());
				crowd.setUnionid(crowdUser.getUnionid());
				crowd.setGameId(crowdUser.getGameId());
				crowdService.updateByPrimaryKeySelective(crowd);

				commMsg.setMsg(" 操作成功~ ");
				commMsg.setCode(GmConstants.STATUS_CODE_SUCCESS);
				return resp(commMsg.build());
			}
			commMsg.setMsg(" 权限不够~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
		} catch (Exception e) {
			commMsg.setMsg(" 操作失败~ ");
			commMsg.setCode(GmConstants.STATUS_CODE_FAILURE);
			logger.error("修改群信息失败", e);
		}
		return resp(commMsg.build());
	}

	/**
	 * 我的牌友群
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/grouplist", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String crowd_my(HttpServletRequest request, ModelMap model) {

		String uid = request.getParameter("uid");

		PpUser user = pUserService.getPpUser(uid);

		com.lzy.crm.proto.Group.PaiQuanList_Info_Record.Builder paiQuanList_Info_Record = PaiQuanList_Info_Record
				.newBuilder();

		// 判断用户信息是否为空
		if (user != null) {

			PageQuery pageQuery = new PageQuery(0, 30);
			List<Crowd> crowdList = crowdService
					.getJoinCrowdListByUnionIdbyPage(user.getUnionid(),
							pageQuery);
			// 我加入的牌圈
			for (Crowd crowd : crowdList) {
				com.lzy.crm.proto.Group.PaiQuan_Info.Builder paiQuan_Info = PaiQuan_Info
						.newBuilder();
				// 获取派群 在玩桌子数
				Integer roomCount = createRoomInfoService
						.getCrowdRoomCount(crowd.getId().toString());
				Integer applyCount = crowdApplyService.getApplyList(
						crowd.getId(), "0");
				paiQuan_Info.setPlayingTableNum(roomCount == null ? 0
						: roomCount);
				paiQuan_Info.setOwnerID(crowd.getGameId());
				paiQuan_Info.setMemberNumber(crowd.getNub());
				paiQuan_Info.setQuanID(crowd.getId());
				paiQuan_Info.setQuanName(crowd.getCrowdName());
				paiQuan_Info
						.setStrPaiQuanExplain(crowd.getRemark() != null ? crowd
								.getRemark() : "");
				paiQuan_Info.setGameRule(crowd.getGameRule() == null ? 0
						: crowd.getGameRule());
				paiQuan_Info.setRuleJson(crowd.getRuleJson() == null ? ""
						: crowd.getRuleJson());
				paiQuan_Info.setJoinVerify(crowd.getIsCard() == null ? 0
						: Integer.parseInt(crowd.getIsCard()));
				paiQuan_Info.setMsgCount(applyCount);
				paiQuan_Info.setEnable(true);
				paiQuan_Info.setQuanZhuName(crowd.getOutUserName());
				paiQuanList_Info_Record.addPaiquanList(paiQuan_Info);
			}
		}

		return resp(paiQuanList_Info_Record.build());
	}

	/**
	 * 获取牌圈桌子数
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/tablelist", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tablelist(HttpServletRequest request, ModelMap model) {

		String crowdId = request.getParameter("groupId");
		com.lzy.crm.proto.Group.PaiQuanRoomList_Info_Record.Builder paiQuanRoomList_Info_Record = PaiQuanRoomList_Info_Record
				.newBuilder();

		try {

			List<CreateRoomInfo> createRoomInfoList = createRoomInfoService
					.createRoomInfoList(crowdId);
			Crowd crowd = crowdService.getById(crowdId);
			com.lzy.crm.proto.Group.PaiQuanRoom_Info.Builder paiQuanRoom_Info = null;
			Integer roomId = 0;
			for (int i = 0; i < createRoomInfoList.size(); i++) {
				CreateRoomInfo createRoomInfo = createRoomInfoList.get(i);
				if (createRoomInfoList.size() == 1
						|| roomId.compareTo(createRoomInfo.getRoomNumber()) != 0) {
					// 添加到集合
					if (i != 0) {
						paiQuanRoomList_Info_Record
								.addKRoomList(paiQuanRoom_Info);
					}
					paiQuanRoom_Info = PaiQuanRoom_Info.newBuilder();
					paiQuanRoom_Info.setManagerUserID(crowd.getGameId());
					paiQuanRoom_Info
							.setKRoomRule(createRoomInfo.getRoomRule() == null ? null
									: createRoomInfo.getRoomRule());
					// paiQuanRoom_Info.setKMemberNumber(createRoomInfo.getRoomMaxPlayer())
					paiQuanRoom_Info.setKRoomNumber(createRoomInfo
							.getRoomNumber() == null ? 0 : createRoomInfo
							.getRoomNumber());
					paiQuanRoom_Info.setKCurrentCount(createRoomInfo
							.getCurrentCount() == null ? 0 : createRoomInfo
							.getCurrentCount());
					paiQuanRoom_Info.setKRoomMaxPlayer(createRoomInfo
							.getRoomMaxPlayer() == null ? 0 : createRoomInfo
							.getRoomMaxPlayer());
					paiQuanRoom_Info
							.setKMaxCount(createRoomInfo.getMaxCount() == null ? 0
									: createRoomInfo.getMaxCount());
					paiQuanRoom_Info.setKCreateRoomUserID(createRoomInfo
							.getCreateRoomUserId());
					paiQuanRoom_Info
							.setRoomIndex(createRoomInfo.getRoomIndex());
				}
				com.lzy.crm.proto.Group.paiquan_player_info.Builder player_info = paiquan_player_info
						.newBuilder();
				// 获取派群 在玩桌子数
				player_info
						.setHeadUrl(createRoomInfo.getNackHead() == null ? ""
								: createRoomInfo.getNackHead());
				player_info.setUserID(createRoomInfo.getUid());
				player_info
						.setName(createRoomInfo.getNickName() == null ? ""
								: createRoomInfo.getNickName());
				paiQuanRoom_Info.addKplayerlist(player_info);
				if (createRoomInfoList.size() - 1 == i) {
					paiQuanRoomList_Info_Record.addKRoomList(paiQuanRoom_Info);
				}
				roomId = createRoomInfo.getRoomNumber();
			}

			// 发送牌圈信息
			com.lzy.crm.proto.Group.PaiQuan_Info.Builder paiQuan_Info = PaiQuan_Info
					.newBuilder();
			Integer applyCount = crowdApplyService.getApplyList(crowd.getId(),
					"0");
			paiQuan_Info.setMsgCount(applyCount);
			paiQuan_Info.setOwnerID(crowd.getGameId());
			paiQuan_Info.setMemberNumber(crowd.getNub());
			paiQuan_Info.setQuanID(crowd.getId());
			paiQuan_Info.setQuanName(crowd.getCrowdName());
			paiQuan_Info.setStrPaiQuanExplain(crowd.getRemark());
			paiQuan_Info.setGameRule(crowd.getGameRule() == null ? 0 : crowd
					.getGameRule());
			paiQuan_Info.setRuleJson(crowd.getRuleJson() == null ? "" : crowd
					.getRuleJson());
			paiQuan_Info.setGameRule(crowd.getIsCard() == null ? 0 : Integer
					.parseInt(crowd.getIsCard()));
			paiQuanRoomList_Info_Record.setPaiquan(paiQuan_Info);
		} catch (Exception e) {
			e.printStackTrace();

		}

		return resp(paiQuanRoomList_Info_Record.build());
	}

	/**
	 * 群成员列表
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/group_members", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String group_members(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.quer_paiquan_player_info_record.Builder player_info_list = quer_paiquan_player_info_record
				.newBuilder();
		String crowdId = request.getParameter("groupid");
		String strPageIndex = request.getParameter("pageIndex");
		Integer pageIndex = 0;
		if (StringUtil.isNotEmpty(strPageIndex)) {
			pageIndex = Integer.parseInt(strPageIndex);
		}
		PageQuery pageQuery = new PageQuery(pageIndex, 30);
		List<CrowdUser> crowdUserList = crowdUserService.getCrowdUserList(
				crowdId, null, pageQuery);
		for (CrowdUser crowdUser : crowdUserList) {
			com.lzy.crm.proto.Group.paiquan_player_info.Builder player_info = paiquan_player_info
					.newBuilder();
			PpUser puser = pUserService.getPpUser(crowdUser.getGameId()
					.toString());
			player_info.setUserID(crowdUser.getGameId());
			player_info.setName(crowdUser.getNickName());
			player_info.setIsManager(crowdUser.getIsManager() != null ? Integer
					.parseInt(crowdUser.getIsManager()) : 0);
			player_info.setHeadUrl(puser != null ? puser.getHeadImgUrl()
					: null);
			player_info_list.addKplayerlist(player_info);
		}
		return resp(player_info_list.build());
	}

	/**
	 * 玩法列表
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(path = "group/private", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String group_rules(HttpServletRequest request, ModelMap model) {
		com.lzy.crm.proto.Group.QuanGameRuleMsgList.Builder ruleList = QuanGameRuleMsgList.newBuilder();
		String crowdId = request.getParameter("groupid");


		List<CrowdRule> crowdRuleList = crowdRuleService.getRuleList(Integer.parseInt(crowdId));
		for (CrowdRule crowdRule : crowdRuleList) {
			com.lzy.crm.proto.Group.QuanGameRuleMsg.Builder rule = QuanGameRuleMsg
					.newBuilder();
			rule.setCreateData(crowdRule.getRule()==null?"":crowdRule.getRule());
			rule.setID(crowdRule.getId());
			rule.setDesc(crowdRule.getRemark()==null?"":crowdRule.getRemark());
			rule.setRuleId(crowdRule.getId().toString());
			ruleList.addRules(rule);
		}
		return resp(ruleList.build());
	}
	
	
}
